
local RWSMod = 1
local MMMod = 1
local SSMod = 1
local CAMod = 0
local MSMod = 1

local	totalRAP = 0
local unbuffedRAP = 0

function HunterPawnOnEvent( event, arg1, arg2, arg3 )
	if ( event == "VARIABLES_LOADED" ) then 
		HunterPawnInitialize()
	end 
	if ( event == "PLAYER_ENTERING_WORLD" ) then
		UpdatePawnScale()
	end
end


function HunterPawnInitialize()
	HunterPawnTooltip = CreateFrame("GameTooltip", "HunterPawnTooltip", UIParent, "GameTooltipTemplate")
	HunterPawnTooltip:SetOwner(UIParent, "ANCHOR_NONE")
	
	UpdatePawnScale()
end

function UpdatePawnScale()
	ParseTalents()
	CalculateUnbuffedRAP()
	
	rapValue = CalculateRAPValue()
	critValue = CalculateCritValue()
	agilityValue = CalculateAgilityValue( rapValue, critValue )
	--hitValue = 15.77
	hitValue = 0.00
	intValue = CalculateIntellectValue( rapValue )
	
	DEFAULT_CHAT_FRAME:AddMessage( rapValue.." "..critValue.." "..agilityValue.." "..hitValue.." "..intValue )
	pawnScale = CreatePawnScale( rapValue, critValue, agilityValue, hitValue, intValue )
	PawnUIImportScaleCallback( pawnScale )
end


function CreatePawnScale( rapValue, critValue, agilityValue, hitValue, intValue )
	PawnDeleteScale( "HunterPawn" )
	socketVal = (10/agilityValue) * 8
	return "(Pawn: v1: \"HunterPawn\": Ap="..10/rapValue..", CritRating="..10/critValue..", Agility="..10/agilityValue..", HitRating="..10/hitValue..", Intellect="..10/intValue..", RedSocket="..socketVal..", BlueSocket="..socketVal..", YellowSocket="..socketVal..")"
end


function CalculateIntellectValue( rapValue )
	if ( CAMod > 0 ) then
		return rapValue / CAMod
	else
		return 0
	end
end


function CalculateAgilityValue( rapValue, critValue )
	return 1 / ((MMMod / rapValue) + ((1/40)*MSMod))
end


function CalculateCritValue()
	return 22.08 / MSMod
end


function CalculateRAPValue()
	totalIncrease = 0
	tempRAP = unbuffedRAP

	local targetDPS = CalculateAutoShotDPS( unbuffedRAP ) * 1.01
	local newDPS = 0
	
	while ( newDPS < targetDPS ) do
		tempRAP = tempRAP + 1
		newDPS = CalculateAutoShotDPS( tempRAP )
	end

	return (tempRAP - unbuffedRAP) / MMMod
end


function CalculateAutoShotDPS( rangedAttackPower )
	weaponSpeed, weaponMinDamage, weaponMaxDamage, ammoDPS, scopeDamage = GetWeaponValues()
	hastedSpeed = weaponSpeed / (1.15*SSMod)

	minDamage = 1*(ammoDPS*weaponSpeed + RWSMod*((rangedAttackPower/14)*weaponSpeed + weaponMinDamage) + scopeDamage)
	maxDamage = 1*(ammoDPS*weaponSpeed + RWSMod*((rangedAttackPower/14)*weaponSpeed + weaponMaxDamage) + scopeDamage)
	weaponDamage = (weaponMinDamage + weaponMaxDamage)/2
	
	avgDamage = (minDamage + maxDamage)/2
	paperDPS = (avgDamage/hastedSpeed)
	
	local autoShotDamage = RWSMod*(ammoDPS*weaponSpeed + ((rangedAttackPower/14)*weaponSpeed) + weaponDamage + scopeDamage)
	autoShotDPS = max(autoShotDamage,1) / hastedSpeed
	return autoShotDPS
end


function GetWeaponValues()
	local speed, minDamage, maxDamage, ammoDPS, scopeDamage = 0, 0, 0, 0, 0

	hasItem = HunterPawnTooltip:SetInventoryItem( "player", 18 )
	for i=1, HunterPawnTooltip:NumLines() do
		ttLeftLine = getglobal("HunterPawnTooltipTextLeft"..i):GetText()
		ttRightLine = getglobal("HunterPawnTooltipTextRight"..i):GetText()
		if ( ttLeftLine ~= nil ) then
			local _, _, val1, val2 = string.find( ttLeftLine, "(%d+) %- (%d+) Damage" )
			if ( val1 and val2 ) then
				minDamage = tonumber( val1 )
				maxDamage = tonumber( val2 )
			end
			local _, _, val = string.find( ttLeftLine, "Scope %(%+(%d+) Damage%)" )
			if ( val ) then
				scopeDamage = tonumber( val )
			end
		end
		if ( ttRightLine ~= nil ) then
			local _, _, val = string.find( ttRightLine, "Speed (%d+%.%d+)" )
			if ( val ) then
				speed = tonumber( val )
			end
		end
	end

	hasItem = HunterPawnTooltip:SetInventoryItem( "player", 0 )
	for i=1, HunterPawnTooltip:NumLines() do
		ttLeftLine = getglobal("HunterPawnTooltipTextLeft"..i):GetText()
		if ( ttLeftLine ~= nil ) then
			local _, _, val = string.find( ttLeftLine, "Adds (%d+) damage per second" )
			if ( val ) then
				ammoDPS = tonumber( val )
			end
		end
	end

	return speed, minDamage, maxDamage, ammoDPS, scopeDamage
end


function ParseTalents()
	local numTabs = GetNumTalentTabs();
	for t=1, numTabs do
	    local numTalents = GetNumTalents(t);
	    for i=1, numTalents do
	        talentName, icon, tier, column, currRank, maxRank= GetTalentInfo(t,i);
	        if ( talentName == "Master Marksman" ) then
	        	MMMod = 1 + currRank * 0.02
	        end
	        if ( talentName == "Ranged Weapon Specialization" ) then
	        	RWSMod = 1 + currRank * 0.01
	        end
	        if ( talentName == "Serpent's Swiftness" ) then
	        	SSMod = 1 + currRank * 0.04
	        end
	        if ( talentName == "Careful Aim" ) then
	        	CAMod = currRank * 0.15
	        end
	        if ( talentName == "Mortal Shots" ) then
	        	MSMod = 1 + currRank * 0.06
	        end
	    end
	end
end


function CalculateUnbuffedRAP()
	local baseRAP, modRAP = UnitRangedAttackPower( "player" )
	totalRAP = baseRAP + modRAP
	unbuffedRAP = totalRAP

	for i=1, 32 do
		if ( UnitBuff( "player", i ) ~= nil ) then
			HunterPawnTooltip:SetUnitBuff( "player", i )
     	local _, _, val = string.find( HunterPawnTooltipTextLeft2:GetText(), "attack power by (%d+)" )
     	if ( val ) then
     		unbuffedRAP = unbuffedRAP - (val * MMMod)	
     	end
     	local _, _, val = string.find( HunterPawnTooltipTextLeft2:GetText(), "Intellect by (%d+)" )
     	if ( val ) then
     		unbuffedRAP = unbuffedRAP - (val * CAMod )
     	end
		end
	end
end


if (not HunterPawnFrame) then
	HunterPawnFrame = CreateFrame("Frame", "HunterPawnFrame")
end

HunterPawnFrame:SetScript("OnEvent", function() HunterPawnOnEvent( event, arg1, arg2, arg3 ) end)
HunterPawnFrame:RegisterEvent("VARIABLES_LOADED")
HunterPawnFrame:RegisterEvent("PLAYER_ENTERING_WORLD")



